package cn.tedu.basic;

import java.sql.*;

public class PracticeJdbc2 {
    public static void main(String[] args) {
        method();

    }

    private static void method() {
        Connection con=null;
        PreparedStatement ps=null;
//        ResultSet rs=null;
        int rownum=0;

        try {

            con = JdbcUtils2.getConnection();
            String sql = "insert into user(name,pwd) values(?,?)";
            //preparedStatement:用来执行SQl,先把SQL骨架发送给数据库执行
            //避免SQL注入的风险,省略了SQL的拼接过程,高效
            ps = con.prepareStatement(sql);
            ps.setString(1, "Mary");
            ps.setString(2, "789");
            rownum = ps.executeUpdate();//专门执行增删改的SQL,返回对数据库的影响行数,返回int整数类型的值;
//            rs = ps.executeQuery();
            if(rownum>0){
                System.out.println("插入成功!");
            }

//            while (rs.next()) {
//                for (int i = 1; i < 4; i++) {
//                    System.out.println(rs.getString(i));
//                }
//            }
        } catch (Exception e) {
            System.out.println("插入失败!");
            e.printStackTrace();
        } finally {
            CloseableUtils2.close(con, ps, null);
        }
    }
}